let questionApp = new Vue({
    el:"#questionApp",
    data:{question:{}},
    methods:{
        loadQuestion:function () {
            //获得url地址栏上面的?后面的内容
            let qid = location.search
            //location.search功能
            //获取url中?以及之后的内容 如果url中没有? qid=null
            //如果url中有? 后面没有值 qid=null
            //如果url中有?并且后面有内容 qid=?{后面的内容}
            if (!qid) {
                //如果qid是空就进这个if
                alert("请指定问题id")
                return
            }
            qid = qid.substring(1);
            axios.get("/v1/questions/" + qid).then(function (response) {
                questionApp.question = response.data
                addDuration(response.data);
            })
        }
        },
    created:function () {
        this.loadQuestion()
    }
})


let postAnswerApp= new Vue({
    el:"#postAnswerApp",
    data:{},
    methods: {
     postAnswer:function () {
         let qid = location.search;
         if (!qid){
             return;
         }
         qid=qid.substring(1)
         /*用户在文本中输入的内容*/
         let content = $("#summernote").val();
         //将问题ID和内容中保存在表单对象中
         let form = new FormData();
         form.append("questionId",qid)
         form.append("content",content)
         axios.post("/v1/answers",form).then(function (response) {
             console.log(response.data)
             let answer = response.data;
             //将控制新增返回的answer对象追加到回答列表中
             answersApp.answers.push(answer)
             //富文本编辑器清空内容
             $("#summernote").summernote("reset");
         })
     }
    }
})




//显示回答列表的JS代码
let answersApp = new Vue({
    el:"#answersApp",
    data:{answers:[]},
    methods:{
        loadAnswers:function () {
            let qid = location.search;
            if (!qid){
                return
            }
            qid = qid.substring(1)
            axios.get("/v1/answers/question/"+qid).then(function (response) {
                answersApp.answers=response.data
                answersApp.updateDuration();
            })

        },
        updateDuration:function () {
            let answers = this.answers;
            for (let i = 0; i < answers.length; i++) {
                addDuration(answers[i])
            }

        },
        postComment:function (answerId) {
            /*获得用户输入的多行文本框*/
            let textarea = $("#addComment"+answerId+" textarea")
            let content = textarea.val();
            //封装表单
            let form = new FormData()
            form.append("answerId",answerId)
            form.append("content",content)
            axios.post("/v1/comments",form).then(function (response) {
                console.log(response.data)
                let  comment = response.data
                let answers = answersApp.answers;
                for (let i = 0; i < answers.length; i++) {
                    /*遍历所有回答,如果当前回答的id和评论新增的id一致的话*/
                   if (answers[i].id==answerId){
                       //将新增成功的评论添加到这个回答的评论列表中
                       answers[i].comments.push(comment)
                       textarea.val("");
                       break;
                   }
                }
            })
        },
        removeComment:function (commentId) {
            axios.get("/v1/comments/delete/"+commentId).then(function (response) {
                console.log(response.data)
            })
        }
    },
    created:function () {
        this.loadAnswers();
    }
})